﻿using Eoffice.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Eoffice.Controllers
{
    public class SearchController : Controller
    {
        EofficeDb data = new EofficeDb();
        //
        // GET: /My_PartialView/

        #region[Tim kiem top]
        [HttpPost]
        public ActionResult timkiemTop(FormCollection collection)
        {
            var search = collection["Search"];
            var seachWord = search.Replace(" ", "+");
            return Redirect("/Pages/timkiem?searchword=" + seachWord + "");
        }
        #endregion

        #region[Tim kiem]
        [ChildActionOnly]
        public ActionResult timkiem()
        {
            return PartialView();
        }
        #endregion

        #region[Tim kiem socvden]
        [ChildActionOnly]
        public ActionResult timkiem_socvden()
        {
            return PartialView();
        }
        #endregion
        #region[Tim kiem socvden]
        [HttpPost]
        public ActionResult search_socv(string query)
        {
            string chuoi = "";
            if (query == "")
            {
                return PartialView();
            }
            else
            {
                List<CVDEN> listData = null;
                //checking the query parameter sent from view. If it is null we will return null else we will return list based on query.
                var list = (from n in data.CVDENs select n).ToList();
                if (!string.IsNullOrEmpty(query))
                {
                    
                    listData = list.Where(q => q.SOCV.ToLower().Contains(query.ToLower())).Take(6).ToList();
                    if (listData.Count > 0)
                    {
                        chuoi += "<ul id='searchUL'>";
                        for (int i = 0; i < listData.Count; i++)
                        {
                            chuoi += "<li>";
                            chuoi += "<a href='/CvDen/Details/" + listData[i].STTCV + "'>";
                            chuoi += "<p>" + FormatContentNews(listData[i].TRICHYEUCVDEN, 50) +"..."+ "</p>";
                            chuoi += "<span>" + listData[i].SOCV;
                            chuoi += "<div class='clear'></div>";
                            chuoi += "</a>";
                            chuoi += "</li>";
                        }
                        chuoi += "</ul>";
                    }
                }
                else
                {
                    chuoi = null;
                }
                //Returning the matched list as json data.
                return Json(new { success = chuoi });
            }
        }
        #endregion

        #region[Tim kiem socvdi]
        [ChildActionOnly]
        public ActionResult timkiem_socvdi()
        {
            return PartialView();
        }
        #endregion

        #region[Tim kiem socvdi]
        [HttpPost]
        public ActionResult search_socvdi(string query)
        {
            string chuoi = "";
            if (query == "")
            {
                return PartialView();
            }
            else
            {
                List<CVDI> listData = null;
                //checking the query parameter sent from view. If it is null we will return null else we will return list based on query.
                var list = (from n in data.CVDIs select n).ToList();
                if (!string.IsNullOrEmpty(query))
                {
                    listData = list.Where(q => q.SOCVDI.ToLower().Contains(query.ToLower())).Take(6).ToList();
                    if (listData.Count > 0)
                    {
                        chuoi += "<ul id='searchUL'>";
                        for (int i = 0; i < listData.Count; i++)
                        {
                            chuoi += "<li>";
                            chuoi += "<a href='/CvDi/Details/" + listData[i].SOCVDI + "'>";
                            chuoi += "<p>" + FormatContentNews(listData[i].TRICHYEUCVDI, 50) + "..." + "</p>";
                            chuoi += "<span>" + listData[i].SOCVDI;
                            chuoi += "<div class='clear'></div>";
                            chuoi += "</a>";
                            chuoi += "</li>";
                        }
                        chuoi += "</ul>";
                    }
                }
                else
                {
                    chuoi = null;
                }
                //Returning the matched list as json data.
                return Json(new { success = chuoi });
            }
        }
        #endregion

        #region[Cat chuoi text de hien thi]
        protected string FormatContentNews(string value, int count)
        {
            string _value = value;
            if (_value.Length >= count)
            {
                string ValueCut = _value.Substring(0, count - 3);
                string[] valuearray = ValueCut.Split(' ');
                string valuereturn = "";
                for (int i = 0; i < valuearray.Length - 1; i++)
                {
                    valuereturn = valuereturn + " " + valuearray[i];
                }
                return valuereturn;
            }
            else
            {
                return _value;
            }
        }
        #endregion

        #region[Thêm dấu Tiếng Việt đối với từng chữ]
        public string ReplaceCharSet(string input)
        {
            string charSet = input.ToLower();
            if (charSet == "a")
                return "[aàảãáạăằẳẵắặâầẩẫấậ]";
            else if (charSet == "e")
                return "[eèẻẽéẹêềểễếệ]";
            else if (charSet == "i")
                return "[iìỉĩíị]";
            else if (charSet == "o")
                return "[oòỏõóọôồổỗốộơờởỡớợ]";
            else if (charSet == "u")
                return "[uùủũúụưừửữứự]";
            else if (charSet == "y")
                return "[yỳỷỹýỵ]";
            else if (charSet == "d")
                return "[dđ]";
            return charSet;
        }
        #endregion

        #region[Loại bỏ dấu Tiếng Việt]
        private static readonly string[] VietNamChar = new string[] 
        { 
            "aAeEoOuUiIdDyY", 
            "áàạảãâấầậẩẫăắằặẳẵ", 
            "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", 
            "éèẹẻẽêếềệểễ", 
            "ÉÈẸẺẼÊẾỀỆỂỄ", 
            "óòọỏõôốồộổỗơớờợởỡ", 
            "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", 
            "úùụủũưứừựửữ", 
            "ÚÙỤỦŨƯỨỪỰỬỮ", 
            "íìịỉĩ", 
            "ÍÌỊỈĨ", 
            "đ", 
            "Đ", 
            "ýỳỵỷỹ", 
            "ÝỲỴỶỸ" 
        };
        public static string LocDau(string str)
        {
            //Thay thế và lọc dấu từng char      
            for (int i = 1; i < VietNamChar.Length; i++)
            {
                for (int j = 0; j < VietNamChar[i].Length; j++)
                    str = str.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
            }
            return str;
        }
        #endregion

    }
}
